/*
 * Copyright (c) 2018. the original author or authors.
 * Kunlun is licensed under the "LICENSE" file in the project's root directory.
 */

package kunlun.ai.support.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * The AI chat message.
 * @author Kahle
 */
public class Message implements Serializable {
    public static final String ASSISTANT = "assistant";
    public static final String SYSTEM    = "system";
    public static final String USER      = "user";
    public static final String TOOL      = "tool";

    /**
     * An optional name for the participant.
     */
    private String name;
    /**
     * The role of the messages author (system, user, assistant, tool).
     */
    private String role;
    /**
     * The contents of the user message.
     */
    private Object content;
    /**
     * The refusal message generated by the model.
     */
    private String refusal;
    /**
     * The tool calls generated by the model, such as function calls.
     */
    private List<ToolCall> toolCalls;

    public Message(String role, Object content) {
        this.content = content;
        this.role = role;
    }

    public Message() {

    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getRole() {

        return role;
    }

    public void setRole(String role) {

        this.role = role;
    }

    public Object getContent() {

        return content;
    }

    public void setContent(Object content) {

        this.content = content;
    }

    public String getRefusal() {

        return refusal;
    }

    public void setRefusal(String refusal) {

        this.refusal = refusal;
    }

    public List<ToolCall> getToolCalls() {

        return toolCalls;
    }

    public void setToolCalls(List<ToolCall> toolCalls) {

        this.toolCalls = toolCalls;
    }

    /**
     * The tool call builder.
     * @author Kahle
     */
    public static class Builder implements kunlun.core.Builder {

        public static Builder ofSystem(Object content) {

            return of(SYSTEM, content);
        }

        public static Builder ofAssistant(Object content) {

            return of(ASSISTANT, content);
        }

        public static Builder ofUser(Object content) {

            return of(USER, content);
        }

        public static Builder ofTool(Object content) {

            return of(TOOL, content);
        }

        public static Builder of(String role, Object content) {

            return of().setRole(role).setContent(content);
        }

        public static Builder of() {

            return new Builder();
        }

        private String name;
        private String role;
        private Object content;
        private String refusal;
        private final List<ToolCall> toolCalls = new ArrayList<ToolCall>();

        public String getName() {

            return name;
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public String getRole() {

            return role;
        }

        public Builder setRole(String role) {
            this.role = role;
            return this;
        }

        public Object getContent() {

            return content;
        }

        public Builder setContent(Object content) {
            this.content = content;
            return this;
        }

        public String getRefusal() {

            return refusal;
        }

        public Builder setRefusal(String refusal) {
            this.refusal = refusal;
            return this;
        }

        public List<ToolCall> getToolCalls() {

            return toolCalls;
        }

        public Builder addToolCalls(List<ToolCall> toolCalls) {
            this.toolCalls.addAll(toolCalls);
            return this;
        }

        public Builder addToolCall(ToolCall toolCall) {
            this.toolCalls.add(toolCall);
            return this;
        }

        @Override
        public Message build() {
            Message message = new Message();
            message.setName(name);
            message.setRole(role);
            message.setContent(content);
            message.setRefusal(refusal);
            message.setToolCalls(toolCalls);
            return message;
        }
    }

}
